package com.zou.music_downloader.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zou.music_downloader.AlbumActivity_;
import com.zou.music_downloader.ArtistActivity_;
import com.zou.music_downloader.DownloadDialogFragment;
import com.zou.music_downloader.R;
import com.zou.music_downloader.bean.DataBean;
import com.zou.music_downloader.bean.SimpleSearchSugestionEntity;
import com.zou.music_downloader.sqlite.MyDao;

import java.util.List;

/**
 * Created by zou on 2016/3/11.
 */
public class SugestionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context                           context;
    List<SimpleSearchSugestionEntity> datas;
    FragmentManager                   fragmentManager;
    Gson gson = new Gson();
    MyDao myDao;


    private final LayoutInflater mLayoutInflater;

    public SugestionAdapter(Context context, List<SimpleSearchSugestionEntity> datas) {
        this.context = context;
        this.datas = datas;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public SugestionAdapter(Context context, List<SimpleSearchSugestionEntity> datas, FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
        this.datas = datas;
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        myDao = new MyDao(context);

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

//        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_search_sugestions, parent, false));
//0：艺术家 1：专辑 2：歌曲
        switch (viewType) {
            case 0:
                return new ArtistViewHodler(mLayoutInflater.inflate(R.layout.item_artist, parent, false));
            case 1:
                return new AlbumViewHodler(mLayoutInflater.inflate(R.layout.item_album, parent, false));
            case 2:
                return new SongViewHodler(mLayoutInflater.inflate(R.layout.item_song, parent, false));
            case 3:
                TitleHodler artistTitle = new TitleHodler(mLayoutInflater.inflate(R.layout.item_title, parent, false));
                artistTitle.title.setText("歌手");
                return artistTitle;
            case 4:
                TitleHodler albumTitle = new TitleHodler(mLayoutInflater.inflate(R.layout.item_title, parent, false));
                albumTitle.title.setText("专辑");
                return albumTitle;
            case 5:
                TitleHodler songTitle = new TitleHodler(mLayoutInflater.inflate(R.layout.item_title, parent, false));
                songTitle.title.setText("歌曲");
                return songTitle;
            default:
                return null;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof SongViewHodler) {
            ((SongViewHodler) holder).textView.setText(datas.get(position).getTitle());
            ((SongViewHodler) holder).songId = datas.get(position).getId();
            ((SongViewHodler) holder).download.setVisibility(View.GONE);

            Log.e("wocao", "onBindViewHolder: " + datas.get(position).getId());
            DataBean dataBean = myDao.alterDateBySongId(datas.get(position).getId());
            if (dataBean != null) {
                ((SongViewHodler) holder).download.setText(dataBean.getState());
                ((SongViewHodler) holder).download.setVisibility(View.VISIBLE);

                Log.e("wocao", "onBindViewHolder: fei null");

            } else {
                ((SongViewHodler) holder).download.setVisibility(View.GONE);
                Log.e("wocao", "onBindViewHolder: null");

            }


        } else if (holder instanceof ArtistViewHodler) {
            ((ArtistViewHodler) holder).textView.setText(datas.get(position).getTitle());
            ((ArtistViewHodler) holder).intent = new Intent(context, ArtistActivity_.class);
            ((ArtistViewHodler) holder).intent.putExtra("id", datas.get(position).getId());
        } else if (holder instanceof AlbumViewHodler) {
            ((AlbumViewHodler) holder).textView.setText(datas.get(position).getTitle());
            ((AlbumViewHodler) holder).intent = new Intent(context, AlbumActivity_.class);
            ((AlbumViewHodler) holder).intent.putExtra("id", datas.get(position).getId());
        }

    }

/*    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
//        holder.imageView.setImageResource(R.mipmap.ic_album_black_36dp);
        holder.imageView.setImageResource(imageType[datas.get(position).getType()]);
        holder.textView.setText(datas.get(position).getTitle());
    }*/

    @Override
    public int getItemViewType(int position) {
        return datas.get(position).getType();
    }

    @Override
    public int getItemCount() {
        if (datas == null) {
            return 0;
        }
        return datas.size();
    }


    class SongViewHodler extends RecyclerView.ViewHolder {
        TextView textView;
        String   songId;
        TextView download;

        public SongViewHodler(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.text_song);
            download = (TextView) itemView.findViewById(R.id.download_song);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    showDialog();

                }

                private void showDialog() {
                    //                    弹出下载dialog
                    FragmentTransaction transaction = fragmentManager.beginTransaction();
                    Bundle              args        = new Bundle();
                    args.putString("id", songId);
                    DialogFragment dialogFragment = new DownloadDialogFragment();
                    dialogFragment.setArguments(args);
                    dialogFragment.show(transaction, "dialog");
                }
            });
        }
    }

    class ArtistViewHodler extends RecyclerView.ViewHolder {
        TextView textView;
        Intent   intent;


        public ArtistViewHodler(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.text_artist);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    context.startActivity(intent);
                }
            });
        }
    }

    class AlbumViewHodler extends RecyclerView.ViewHolder {
        TextView textView;
        Intent   intent;

        public AlbumViewHodler(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.text_album);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    context.startActivity(intent);
                }
            });
        }
    }

    class TitleHodler extends RecyclerView.ViewHolder {
        TextView title;

        public TitleHodler(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.text_title);
        }
    }
}
